package com.aji.leather1.controller;

import com.aji.allcommon.result.Result;
import com.aji.allcommon.service.FileStorageService;
import com.aji.allcommon.utills.MultipartFileUtill;
import com.aji.leather1.config.FileEnvConfig;
import com.aji.leather1.domain.Leather;
import com.aji.leather1.domain.Sep;
import com.aji.leather1.domain.dto.LeatherDto;
import com.aji.leather1.mapper.SepMapper;
import com.aji.leather1.service.LeatherService;
import com.aji.leather1.service.SepService;
import com.aji.leather1.utils.FileUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/leather1")
@Api(tags = "现代皮革样品模块")
@CrossOrigin
@RequiredArgsConstructor
public class LeatherController {

    private final LeatherService leatherService;
    private final FileStorageService fileStorageService;
    private final SepService sepService;
    private final SepMapper sepMapper;
    private final FileEnvConfig fileEnvConfig;



    @ApiOperation("图片转url地址")
    @PostMapping("/imgToUrl")
    public Result imgToUrl(MultipartFile imgFile) throws IOException {
        String originalFilename = imgFile.getOriginalFilename();
        String simpFileName = originalFilename.substring(0, originalFilename.length() - 4) + ".jpg";
        File fileimg = File.createTempFile("temp", null);
        imgFile.transferTo(fileimg);
        FileInputStream simpJpgFile = MultipartFileUtill.multipartTifToJpgFileInputStream(fileimg);
        String accessSimpFileName = fileStorageService.uploadFile("img/jpg", simpFileName, simpJpgFile, "img/jpg");
        return Result.success(accessSimpFileName);
    }
    @ApiOperation("新增皮革信息")
    @PostMapping()
    public Result save(@RequestParam("file") MultipartFile file, String leatherNumber, String pictureUrl){
        leatherService.add(file, leatherNumber, pictureUrl);
        return Result.success("新增成功！");
    }
    @ApiOperation("删除皮革信息")
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        QueryWrapper<Sep> sepWrapper = new QueryWrapper<>();
        sepWrapper.in("leather_id", ids);
        sepMapper.delete(sepWrapper);
        leatherService.removeByIds(ids);
        return Result.success("删除成功！");
    }
    @ApiOperation("修改样品信息")
    @PutMapping()
    public Result updateById(@RequestBody Leather entity){
        System.out.println(entity);
        leatherService.updateById(entity);
        return Result.success();
    }
    @ApiOperation("查询皮革信息 根据id")
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id){
        return Result.success(leatherService.getById(id));
    }

    @ApiOperation("根据皮革id查询光谱")
    @GetMapping("/leather/{id}")
    public Result getSepByLeatherId(@PathVariable Long id){
        LambdaQueryWrapper<Sep> compoundLambdaQueryWrapper  =new LambdaQueryWrapper<Sep>().select()
                .eq(id!=null,Sep::getLeatherId,id);
        List<Sep> sepList = sepService.list(compoundLambdaQueryWrapper);
        return Result.success(sepList);
    }

    @ApiOperation("查询全部皮革信息")
    @GetMapping("/list")
    public Result list(){
        return Result.success(leatherService.list(null));
    }

    @ApiOperation("条件分页查询")
    @GetMapping("/{page}/{pageSize}")
    public Result listByConditionAndPage(@PathVariable int page, @PathVariable int pageSize, LeatherDto compound){
        //合法检验
        if(page<1||pageSize<1){return Result.error("分页参数异常");}
        Page<Leather> compoundPage= new Page<>(page,pageSize);
        LambdaQueryWrapper<Leather> compoundLambdaQueryWrapper  =new LambdaQueryWrapper<Leather>().select()
                .like(compound.getLeatherNumber()!=null,Leather::getLeatherNumber,compound.getLeatherNumber())
                .like(compound.getLeatherName()!=null,Leather::getLeatherName,compound.getLeatherName())
                .like(compound.getAgingType()!=null,Leather::getAgingType,compound.getAgingType())
                .like(compound.getAgingDays()!=null,Leather::getAgingDays,compound.getAgingDays())
                .like(compound.getDegradationDays()!=null,Leather::getDegradationDays,compound.getDegradationDays())
                .like(compound.getRepeatGroup()!=null,Leather::getRepeatGroup,compound.getRepeatGroup());

        IPage<Leather> compoundIPage = leatherService.page(compoundPage, compoundLambdaQueryWrapper);
        return Result.success(compoundIPage);
    }

    @ApiOperation("获取 demo文件")
    @GetMapping("/demoFile")
    public ResponseEntity<byte[]> getDemoFile() throws IOException {
        // 拼接文件路径
        String fullPath = fileEnvConfig.getDataPath() + fileEnvConfig.getDemoFile();
        String fileName = fileEnvConfig.getDemoFile();

        // 使用工具类读取文件为 byte[]
        return FileUtils.transformFile(fullPath, fileName);
    }

    @ApiOperation("获取数据总量")
    @GetMapping("/count")
    public Result count(){
        int count = Math.toIntExact(leatherService.count());
        return Result.success(count);
    }
}
